package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.Travel;
import cn.wolfcode.luowowo.article.domain.TravelContent;
import cn.wolfcode.luowowo.article.mapper.DestinationMapper;
import cn.wolfcode.luowowo.article.mapper.TravelContentMapper;
import cn.wolfcode.luowowo.article.mapper.TravelMapper;
import cn.wolfcode.luowowo.article.query.TravelQueryObject;
import cn.wolfcode.luowowo.article.service.ITravelService;
import cn.wolfcode.luowowo.common.exception.LogistException;
import cn.wolfcode.luowowo.common.query.QueryObject;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

@Service
public class TravelServiceImpl implements ITravelService {
    @Autowired
    private TravelMapper travelMapper;
    @Autowired
    private TravelContentMapper travelContentMapper;
    @Autowired
    private DestinationMapper destinationMapper;


    @Override
    public int deleteByPrimaryKey(Long id) {
        return 0;
    }

    @Override
    public int insert(Travel record) {
        return 0;
    }

    @Override
    public Travel selectByPrimaryKey(Long id) {
        return travelMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Travel> selectAll() {
        return null;
    }

    @Override
    public int updateByPrimaryKey(Travel record) {
        return 0;
    }

    @Override
    public PageInfo<Travel> query(TravelQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        return new PageInfo<>(travelMapper.queryForList(qo));
    }

    @Override
    public Long saveOrUpdate(Travel travel)throws LogistException {
        //目的地
        Destination dest = destinationMapper.selectByPrimaryKey(travel.getDest().getId());
        travel.setDest(dest);
        //简介
        String content = travel.getTravelContent().getContent();
        if (content.length()>200){
            travel.setSummary(content.substring(0,200));
        }else{
            travel.setSummary(content);
        }
        //
        if (travel.getId()==null){
            travel.setCreateTime(new Date());
            travelMapper.insert(travel);
            TravelContent travelContent = travel.getTravelContent();
            travelContent.setId(travel.getId());
            travelContentMapper.insert(travelContent);
        }else {
            travel.setLastUpdateTime(new Date());
            travelMapper.updateByPrimaryKey(travel);
            TravelContent travelContent = travel.getTravelContent();
            travelContent.setId(travel.getId());
            travelContentMapper.update(travelContent);
        }
        return travel.getId();
    }

    @Override
    public TravelContent getContent(Long id) {
        return travelContentMapper.getContent(id);
    }

    @Override
    public List<Travel> getViewNumTop3(Long id) {
        return travelMapper.getViewNumTop3(id);
    }

    @Override
    public List<Travel> list() {
        return travelMapper.selectAll();
    }

    @Override
    public PageInfo<Travel> getByAuthorId(Long authorId, TravelQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        return new PageInfo<>(travelMapper.selectByAuthorId(authorId, qo));
    }

    @Override
    public Travel queryById(Long tid) {
        return travelMapper.selectByPrimaryKey(tid);
    }

    @Override
    public List<Long> queryByAuthor(Long userId) {
        return travelMapper.selectByAuthor(userId);
    }
}
